package com.sys.midware.assist.utils.json;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSON;

/**
 * json 工具包,底层采用 jackson
 * 
 * @ClassName: JsonUtil
 * @author jerome
 * @author yellowhat.huang
 * @date 2012-3-19 下午06:17:39
 * @Description json常用静态方法
 */
public class JsonUtil {

    /**
     * json check
     */
    private final static String LEFT_DKH = "{";

    private final static String RIGHT_DKH = "}";

     /**
     * objectMapper
     */
     private static ObjectMapper objectMapper = new ObjectMapper();
    
     static {
     objectMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
     objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
     objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
     true);
     objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
     true);
     }

    /**
     * 把对象转成json格式，放到response输出流中
     * 
     * @param response
     * @param object
     * @throws Exception
     */
    public static String toJSONString(Object object) throws Exception {
        try {
            return JSON.toJSONString(object);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 读取json，转化为java对象
     * 
     * @throws Exception
     */
    public static Object toJavaObject(String jsonString, Class<?> clazz) throws Exception {
        try {
            if (jsonString == null || "".equals(jsonString.trim())) {
                return null;
            }
            return JSON.toJavaObject(JSON.parseObject(jsonString), clazz);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 读取json字节流，转化为java对象
     * 
     * @throws Exception
     */
    public static Object readJson(byte[] values, int offset, int length, Class<?> clazz) throws Exception {
        try {
            if (values == null || offset < 0 || offset >= length) {
                return null;
            }
            return objectMapper.readValue(values, offset, length, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * read jsonStr，convert to map object ,and get attrName value
     * 
     * @param jsonString
     * @param attrName
     * @return attrName => value
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static String readJson(String jsonString, String attrName) throws Exception {
        try {
            if (StringUtils.isBlank(jsonString) || StringUtils.isBlank(attrName)) {
                return null;
            }
            Map<String, String> jsonMap = objectMapper.readValue(jsonString.trim(), Map.class);
            if (null != jsonMap && !jsonMap.isEmpty()) {
                return jsonMap.get(attrName.trim());
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }

    /**
     * check is json string
     * 
     * @param jsonStr
     * @return true is json string
     */
    public static boolean isJsonStr(String jsonStr) {
        if (StringUtils.isBlank(jsonStr)) {
            return false;
        }
        if (jsonStr.startsWith(LEFT_DKH) && jsonStr.endsWith(RIGHT_DKH)) {
            return true;
        }
        return false;
    }
}
